package CBBOT::Config;

use strict;
use warnings;
use DBI;

our $home = "/home/services/cbbot/2.0/";
our $config = "/home/services/cbbot_config";
our %config = read_config($config);

our $dbh = DBI->connect("dbi:mysql:" . "dbname=" .
	$config{mysql_db} . ";host=" . $config{mysql_host},
	$config{mysql_user}, $config{mysql_pass})
	or die "Couldn't connect to MySQL-database: " . DBI->errstr;

sub get_dbh {
	if(!(defined($dbh) && $dbh->ping)) {
		unless ($dbh = DBI->connect("dbi:mysql:" . "dbname=" .
			$config{mysql_db} . ";host=" . $config{mysql_host},
			$config{mysql_user}, $config{mysql_pass})) {
				$dbh = undef;
				die "Couldn't connect to MySQL-database: " . DBI->errstr;
        	}
	}
	return $dbh;
}

sub read_config {
	my $config = shift;
    my %config;
    if(!(-r $config)) {
    	die("Couldn't read config file, dying..\n");
    }
    open CONFIG, '<', $config or die $!;
    while(<CONFIG>) {
        next if (($_ eq "\n") || /^#/);
        chomp;
        my($setting, $value) = split(/=/, $_);
        $config{$setting} = $value;
    }
    return %config;
}

1;

